﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Brain.Rendering
{
    public class Shape_Line3D : Shape
    {
        public VertexPositionColor[] Vertices { get; protected set; }

        public bool UseVertexColor { get; set; }

        public Shape_Line3D()
        {
            Vertices = new VertexPositionColor[2];
            Vertices[0] = new VertexPositionColor(Vector3.Zero, Color.White);
            Vertices[1] = new VertexPositionColor(Vector3.Forward, Color.White);

            UseVertexColor = true;
        }

        public void Draw(object obj, Color color, ICamera3D Camera, ShapeDrawer drawer, Effect effect)
        {
            Line3D line = (Line3D)obj;

            GraphicsDevice device = Engine.Instance.GraphicsDevice;

            //if (line.Thickness == 0)
            //{
            //BasicEffect.DiffuseColor = line.Color.ToVector3();
            //BasicEffect.VertexColorEnabled = true;

            Vertices[0].Position = line.Start;
            Vertices[0].Color = line.Color;

            Vertices[1].Position = line.End;
            Vertices[1].Color = line.Color;

            //BasicEffect.View = View;
            //BasicEffect.Projection = Projection;
            //BasicEffect.World = Matrix.Identity;
            //BasicEffect.CurrentTechnique.Passes[0].Apply();
            for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
            {
                EffectPass pass = effect.CurrentTechnique.Passes[i];

                pass.Apply();
                device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList,
                    this.Vertices, 0, 1);
            }
            /*}
            else
            {
                if (i == 5)
                {
                }

                ThickLineEffect.DiffuseColor = line.Color.ToVector3();

                ThickLineEffect.VertexColorEnabled = false;
                ThickLineEffect.EnableDefaultLighting();
                ThickLineEffect.PreferPerPixelLighting = true;

                BrainModelMesh mesh = Cylinder.Meshes[0];

                Vector3 dir = Vector3Util.Direction(line.Start, line.End);
                Matrix m = Matrix.CreateFromAxisAngle(dir, MathHelper.ToRadians(90));

                float length = Vector3.Distance(line.Start, line.End);

                Matrix k = Matrix.CreateWorld(line.Start, dir, Vector3.Up);

                /*ThickLineEffect.World = Matrix.CreateScale(line.Thickness, length, line.Thickness)
                    * Matrix.CreateRotationX(MathHelper.ToRadians(90)) * k *
                    Matrix.CreateTranslation(Vector3.Up * (length / 2f) + k.Forward * (length / 2f));*/
            /*ThickLineEffect.World = Matrix.CreateScale(line.Thickness, length, line.Thickness)
                * Matrix.CreateRotationX(MathHelper.ToRadians(90)) * k *
                Matrix.CreateTranslation(Vector3.Up + (k.Forward * (length / 3f)));
            ThickLineEffect.CurrentTechnique.Passes[0].Apply();

            device.SetVertexBuffer(mesh.VertexBuffer);
            device.Indices = mesh.IndexBuffer;

            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mesh.NumVertices, 0, mesh.PrimitiveCount);
        }*/
        }

    }
}
